package com.ruoyi.business.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.ruoyi.business.domain.ServiceItem;
import com.ruoyi.business.service.IServiceItemService;
import com.ruoyi.business.service.ProcessService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.apache.commons.io.IOUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.CarPackageAudit;
import com.ruoyi.business.service.ICarPackageAuditService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;

/**
 * 审核列表Controller
 *
 * @author x1ao
 * @date 2021-07-21
 */
@Controller
@RequestMapping("/business/carPackageAudit")
public class CarPackageAuditController extends BaseController {
    private String prefix = "business/carPackageAudit";

    @Autowired
    private ICarPackageAuditService carPackageAuditService;
    @Autowired
    private ProcessService processService;
    @Autowired
    IServiceItemService serviceItemService;

    @RequiresPermissions("business:carPackageAudit:view")
    @GetMapping()
    public String carPackageAudit() {
        return prefix + "/carPackageAudit";
    }

    /**
     * 查询审核列表列表
     */
    @RequiresPermissions("business:carPackageAudit:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CarPackageAudit carPackageAudit) {
        startPage();
        List<CarPackageAudit> list = carPackageAuditService.selectCarPackageAuditList(carPackageAudit);
        return getDataTable(list);
    }

    /**
     * 导出审核列表列表
     */
    @RequiresPermissions("business:carPackageAudit:export")
    @Log(title = "审核列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CarPackageAudit carPackageAudit) {
        List<CarPackageAudit> list = carPackageAuditService.selectCarPackageAuditList(carPackageAudit);
        ExcelUtil<CarPackageAudit> util = new ExcelUtil<CarPackageAudit>(CarPackageAudit.class);
        return util.exportExcel(list, "审核列表数据");
    }

    /**
     * 新增审核列表
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存审核列表
     */
    @RequiresPermissions("business:carPackageAudit:add")
    @Log(title = "审核列表", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CarPackageAudit carPackageAudit) {
        return toAjax(carPackageAuditService.insertCarPackageAudit(carPackageAudit));
    }

    /**
     * 修改审核列表
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        CarPackageAudit carPackageAudit = carPackageAuditService.selectCarPackageAuditById(id);
        mmap.put("carPackageAudit", carPackageAudit);
        return prefix + "/edit";
    }

    /**
     * 修改保存审核列表
     */
    @RequiresPermissions("business:carPackageAudit:edit")
    @Log(title = "审核列表", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CarPackageAudit carPackageAudit) {
        return toAjax(carPackageAuditService.updateCarPackageAudit(carPackageAudit));
    }

    /**
     * 删除审核列表
     */
    @RequiresPermissions("business:carPackageAudit:remove")
    @Log(title = "审核列表", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(carPackageAuditService.deleteCarPackageAuditByIds(ids));
    }

    /**
     * 审核列表查看流程进度图
     *
     * @param id
     */
    @RequestMapping("/processImg/{id}")
    public void processImg(@PathVariable("id") Long id, HttpServletResponse response) throws IOException {
        CarPackageAudit audit = carPackageAuditService.selectCarPackageAuditById(id);
        InputStream inputStream = null;
        if (audit.getStatus().equals(CarPackageAudit.STATUS_IN_ROGRESS) ||
                audit.getStatus().equals(CarPackageAudit.STATUS_REJECT)) {
            //拿活动节点
            List<String> activeActivityIds = processService.getActiveActivityIds(audit.getInstanceId());
            inputStream = processService.getProcessImage(audit.getProcessDefinitionId(),activeActivityIds, Collections.EMPTY_LIST);
        }else {
            inputStream = processService.getProcessImage(audit.getProcessDefinitionId(),Collections.EMPTY_LIST, Collections.EMPTY_LIST);
        }
        IOUtils.copy(inputStream,response.getOutputStream());
    }

    @ResponseBody
    @PostMapping("/cancelApply")
    public AjaxResult cancelApply(String instanceId){
        carPackageAuditService.cancelApply(instanceId);
        return AjaxResult.success();
    }

    @GetMapping("/todoPage")
    public String todoPage(){
        return prefix + "/todoPage";
    }

    /**
     * 查询待办列表
     */
    @PostMapping("/todoList")
    @ResponseBody
    public TableDataInfo todoList(CarPackageAudit carPackageAudit) {
        startPage();
        List<CarPackageAudit> list = carPackageAuditService.selectTodoList(carPackageAudit);
        return getDataTable(list);
    }

    @RequestMapping("/auditDialogPage/{taskId}")
    public String auditDialogPage(@PathVariable("taskId") String taskId,ModelMap mmap){
        mmap.put("taskId",taskId);
        return prefix + "/taskVerify";
    }


    @RequestMapping("/complete/{taskId}")
    public void complete(@PathVariable("taskId") String taskId,Boolean auditStatus,String comment){
        carPackageAuditService.complate(taskId,auditStatus,comment);
    }
}
